package zjhr.com.controller;

import java.io.IOException;
import java.util.List;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import zjhr.com.pojo.EasyUIPageQuery;
import zjhr.com.pojo.ExpenseZj;
import zjhr.com.service.ExpenseService;
import zjhr.com.utils.FileUtils;
import zjhr.com.utils.JsonUtils;

@Controller
public class ExpenseController {
	@Autowired
	private ExpenseService expenseService;
	//添加业务部
	@RequestMapping(value="/addBusinessExpenseInfo.action",method=RequestMethod.POST)
	public String save(ExpenseZj expense){
		String bxjob = expense.getBxjob();
		if(bxjob.equals("0")){
			expenseService.save(expense);
			return "/base/yewubaoxiao.jsp";
		}else if(bxjob.equals("1")){
			expenseService.save(expense);
			return "/human/humanbaoxiao.jsp";
		}else if(bxjob.equals("2")){
			expenseService.save(expense);
			return "/anzhi/staffbaoxiao.jsp";
		}else{
			return null;
		}
	}
	//分页
	@RequestMapping("/ExpensePageQuery.action")
	@ResponseBody
	public String pageQuery(Integer page,Integer rows){
		EasyUIPageQuery pageQuery = expenseService.pageQuery(page, rows);
		return JsonUtils.objectToJson(pageQuery);
	}
	
	//修改
		@RequestMapping(value="/editExpenseInfo.action", method=RequestMethod.POST)
		public String edit(ExpenseZj expense){
			String bxjob = expense.getBxjob();
			expenseService.edit(expense);
			if(bxjob.equals("0")){
				return "/base/yewubaoxiao.jsp";
			}else if(bxjob.equals("1")){
				return "/human/humanbaoxiao.jsp";
			}else if(bxjob.equals("2")){
				return "/anzhi/staffbaoxiao.jsp";
			}else{
				return "/caiwu/baoxiao.jsp";
			}
			
		}
		
		//添加
		@RequestMapping(value="/addExpenseInfo.action", method=RequestMethod.POST)
		public String update(ExpenseZj expense){
			expenseService.save(expense);
			return "/caiwu/baoxiao.jsp";
		}
		//更改
		@RequestMapping("/updateAccountTypt.action")
		public String updateType(String ids){
			expenseService.updateType(ids);
			return "/caiwu/baoxiao.jsp";
		}
		
		//审批
		@RequestMapping(value="/checkExpenseInfo.action", method=RequestMethod.POST)
		public String checkAccountType(ExpenseZj expense){
			
			if(StringUtils.isBlank(expense.getBxid())){
				return "/caiwu/baoxiao.jsp";
			}else{
				expenseService.checkType(expense);
				return "/caiwu/baoxiao.jsp";
			}
		}
		//各大部门的分页
		@RequestMapping("/ExpensePageQueryByBusiness.action")
		@ResponseBody
		public String ExpensePageQueryByBusiness(Integer page,Integer rows,String bxJob){
			if(StringUtils.isNotBlank(bxJob)){
				EasyUIPageQuery query = expenseService.pageQueryByBusiness(page, rows,bxJob);
				return JsonUtils.objectToJson(query);
			}else{
				
				return JsonUtils.objectToJson(null);
			}
		}
		//根据时间导出
		@RequestMapping("/exportExpenseInfo.action")
		public void exportExpense(String beginDate,String endDate,HttpServletRequest request,HttpServletResponse response) throws Exception{
			List<ExpenseZj> list = expenseService.export(beginDate, endDate);
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet("报销数据表");
			HSSFRow headRow = sheet.createRow(0);
			headRow.createCell(0).setCellValue("编号");
			headRow.createCell(1).setCellValue("报销人姓名");
			headRow.createCell(2).setCellValue("报销金额");
			headRow.createCell(3).setCellValue("入职日期");
			headRow.createCell(4).setCellValue("员工状态");
			headRow.createCell(5).setCellValue("报销原因 ");
			headRow.createCell(6).setCellValue("报销部门 ");
			headRow.createCell(7).setCellValue("报销日期 ");
			headRow.createCell(8).setCellValue("报销状态");
			headRow.createCell(9).setCellValue("审批备注 ");
			for (ExpenseZj expenseZj : list) {
				HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
				dataRow.createCell(0).setCellValue(expenseZj.getAccountid());
				dataRow.createCell(1).setCellValue(expenseZj.getAccountname());
				dataRow.createCell(2).setCellValue(expenseZj.getBxmoney());
				dataRow.createCell(3).setCellValue(expenseZj.getExpdate());
				String stafftype = expenseZj.getStafftype();
				if(stafftype.equals("1")){
					dataRow.createCell(4).setCellValue("在职");
				}else if(stafftype.equals("0")){
					dataRow.createCell(4).setCellValue("离职");
				}else{
					dataRow.createCell(4).setCellValue("未填写");
				}
				dataRow.createCell(5).setCellValue(expenseZj.getExpComment());
				String bxjob = expenseZj.getBxjob();
				if(bxjob.equals("0")){
					dataRow.createCell(6).setCellValue("业务部");
				}else if(bxjob.equals("1")){
					dataRow.createCell(6).setCellValue("人力资源部");
				}else if(bxjob.equals("2")){
					dataRow.createCell(6).setCellValue("安置部");
				}else if(bxjob.equals("3")){
					dataRow.createCell(6).setCellValue("财务部");
				}else{
					dataRow.createCell(4).setCellValue("未填写");
				}
				dataRow.createCell(7).setCellValue(expenseZj.getAccountdate());
				String accouttype = expenseZj.getAccouttype();
				//报销状态0:未审批1：审批通过2：审批不通过
				if(accouttype.equals("2")){
					dataRow.createCell(8).setCellValue("审批不通过");
				}else if(accouttype.equals("1")){
					dataRow.createCell(8).setCellValue("审批通过");
				}else{
					dataRow.createCell(8).setCellValue("未审批");
				}
				dataRow.createCell(9).setCellValue(expenseZj.getAccountcomment());
				
			}
			String filename="报销数据.xls";
			String contentType = new MimetypesFileTypeMap().getContentType(filename);//获取文件类型
			ServletOutputStream out = response.getOutputStream();
			response.setContentType(contentType);
			
			//获取客户端浏览器类型
			String agent = request.getHeader("User-Agent");
			filename = FileUtils.encodeDownloadFilename(filename, agent);
			response.setHeader("content-disposition", "attachment;filename="+filename);
			workbook.write(out);
			out.close();
		}
		

}
